<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>ecg_qc.sqi_computing.sqi_power_spectrum &mdash; ecg_qc 1.0.0 documentation</title>
      <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
      <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <!--[if lt IE 9]>
    <script src="../../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
        <script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
        <script src="../../../_static/jquery.js"></script>
        <script src="../../../_static/underscore.js"></script>
        <script src="../../../_static/doctools.js"></script>
    <script src="../../../_static/js/theme.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
            <a href="../../../index.html" class="icon icon-home"> ecg_qc
          </a>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <p class="caption" role="heading"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../ecg_qc.html">API documentation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../references.html">References</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">ecg_qc</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../../../index.html" class="icon icon-home"></a> &raquo;</li>
          <li><a href="../../index.html">Module code</a> &raquo;</li>
      <li>ecg_qc.sqi_computing.sqi_power_spectrum</li>
      <li class="wy-breadcrumbs-aside">
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <h1>Source code for ecg_qc.sqi_computing.sqi_power_spectrum</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>


<div class="viewcode-block" id="psqi"><a class="viewcode-back" href="../../../ecg_qc.html#ecg_qc.sqi_computing.sqi_power_spectrum.psqi">[docs]</a><span class="k">def</span> <span class="nf">psqi</span><span class="p">(</span><span class="n">ecg_signal</span><span class="p">:</span> <span class="nb">list</span><span class="p">,</span> <span class="n">sampling_frequency</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Computes the power spectrum Distribution of QRS Wave.</span>

<span class="sd">    It corresponds to the ratio of the energy of the QRS wave and the energy of</span>
<span class="sd">    the ECG signal. The energy of the QRS wave is computed on frequencies</span>
<span class="sd">    ranging from 5Hz to 15Hz, the energy of the ECG signal is computed on</span>
<span class="sd">    frequencies ranging from 5Hz to 40Hz.</span>

<span class="sd">    If interference exists, the high-frequency component increases, and pSQI</span>
<span class="sd">    decreases.</span>


<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    ecg_signal : list</span>
<span class="sd">        Input ECG signal</span>
<span class="sd">    sampling_frequency : list</span>
<span class="sd">        Input ecg sampling frequency</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    p_sqi_score : float</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">ecg_signal</span><span class="p">)</span>
    <span class="n">t</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">/</span> <span class="n">sampling_frequency</span>

    <span class="n">yf</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">fft</span><span class="o">.</span><span class="n">fft</span><span class="p">(</span><span class="n">ecg_signal</span><span class="p">)</span>
    <span class="n">xf</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="o">/</span><span class="p">(</span><span class="mf">2.0</span><span class="o">*</span><span class="n">t</span><span class="p">),</span> <span class="n">n</span><span class="o">//</span><span class="mi">2</span><span class="p">)</span>

    <span class="n">pds_num</span> <span class="o">=</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">yf</span><span class="p">[</span><span class="n">idx</span><span class="p">])</span> <span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">xf</span><span class="p">))</span> <span class="k">if</span>
               <span class="n">xf</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span> <span class="o">&gt;=</span> <span class="mi">5</span> <span class="ow">and</span> <span class="n">xf</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span> <span class="o">&lt;=</span> <span class="mi">15</span><span class="p">]</span>
    <span class="n">pds_denom</span> <span class="o">=</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">yf</span><span class="p">[</span><span class="n">idx</span><span class="p">])</span> <span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">xf</span><span class="p">))</span> <span class="k">if</span>
                 <span class="n">xf</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span> <span class="o">&gt;=</span> <span class="mi">5</span> <span class="ow">and</span> <span class="n">xf</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span> <span class="o">&lt;=</span> <span class="mi">40</span><span class="p">]</span>
    <span class="n">p_sqi_score</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="nb">sum</span><span class="p">(</span><span class="n">pds_num</span><span class="p">)</span> <span class="o">/</span> <span class="nb">sum</span><span class="p">(</span><span class="n">pds_denom</span><span class="p">),</span> <span class="mi">3</span><span class="p">))</span>

    <span class="k">return</span> <span class="n">p_sqi_score</span></div>


<div class="viewcode-block" id="bassqi"><a class="viewcode-back" href="../../../ecg_qc.html#ecg_qc.sqi_computing.sqi_power_spectrum.bassqi">[docs]</a><span class="k">def</span> <span class="nf">bassqi</span><span class="p">(</span><span class="n">ecg_signal</span><span class="p">:</span> <span class="nb">list</span><span class="p">,</span> <span class="n">sampling_frequency</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Computes the relative power in the Baseline.</span>

<span class="sd">    It corresponds to the ratio of the energy of the QRS wave and the energy of</span>
<span class="sd">    the ECG signal. The energy of the baseline is computed on frequencies</span>
<span class="sd">    ranging from 0Hz to 1Hz, the energy of the ECG signal is computed on</span>
<span class="sd">    frequencies ranging from 0Hz to 40Hz.</span>

<span class="sd">    If there is no baseline drift interference, the basSQI value is close to 1.</span>
<span class="sd">    An abnormal shift in the baseline causes the bassqi to decrease.</span>


<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    ecg_signal : list</span>
<span class="sd">        Input ECG signal</span>
<span class="sd">    sampling_frequency : list</span>
<span class="sd">        Input ecg sampling frequency</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    bas_sqi_score : float</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">ecg_signal</span><span class="p">)</span>
    <span class="n">t</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">/</span> <span class="n">sampling_frequency</span>

    <span class="n">yf</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">fft</span><span class="o">.</span><span class="n">fft</span><span class="p">(</span><span class="n">ecg_signal</span><span class="p">)</span>
    <span class="n">xf</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="o">/</span><span class="p">(</span><span class="mf">2.0</span><span class="o">*</span><span class="n">t</span><span class="p">),</span> <span class="n">n</span><span class="o">//</span><span class="mi">2</span><span class="p">)</span>

    <span class="n">pds_num</span> <span class="o">=</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">yf</span><span class="p">[</span><span class="n">idx</span><span class="p">])</span> <span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">xf</span><span class="p">))</span> <span class="k">if</span>
               <span class="n">xf</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span> <span class="o">&gt;=</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">xf</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span> <span class="o">&lt;=</span> <span class="mi">1</span><span class="p">]</span>
    <span class="n">pds_denom</span> <span class="o">=</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">yf</span><span class="p">[</span><span class="n">idx</span><span class="p">])</span> <span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">xf</span><span class="p">))</span> <span class="k">if</span>
                 <span class="n">xf</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span> <span class="o">&gt;=</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">xf</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span> <span class="o">&lt;=</span> <span class="mi">40</span><span class="p">]</span>

    <span class="n">bas_sqi_score</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="p">(</span><span class="nb">sum</span><span class="p">(</span><span class="n">pds_num</span><span class="p">)</span> <span class="o">/</span> <span class="nb">sum</span><span class="p">(</span><span class="n">pds_denom</span><span class="p">)),</span> <span class="mi">3</span><span class="p">))</span>

    <span class="k">return</span> <span class="n">bas_sqi_score</span></div>
</pre></div>

           </div>
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2021, Aura Healthcare.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>